﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace SignalLab.BaseLibrary
{
  public class VisualiserHelper
  {
    public WriteableBitmap Bitmap;
    int halfHeight;
    int width;

    public VisualiserHelper(int width, int height, long totalbytes)
    {

      halfHeight = height / 2;
      this.width = width;
      samplesPerPixel = (int)Math.Ceiling(totalbytes / 2 / (double)width);
      zoom = height / (double)ushort.MaxValue;
      x = 0;
    }

    int samplesPerPixel;
    double zoom;

    int x;
    int rest;
    int color = 255 << 24 | 255 << 16;

    public void Write(byte[] data)
    {
      int samples = data.Length / 2;

      for (int i = 0; i < samples; i++)
      {
        var sample = Helper.GetSampleAtPosition(data, i * 2);
        int y = (int)(sample * zoom) + halfHeight;
        Bitmap.Pixels[y * width + x] = color;

        if ((i + rest) % samplesPerPixel == 0) x++;

      }
    }
  }
}
